// JavaScript Document
$(function(){
		   var arrShopId = [];
		   var arrImg = [];
		   var arrInfo = [];
		   var T_num = 0
		   // 创建攻略事件 
		   $(".build-gl").live("click",function(){
						if($.cookie('zy_token') == "" || $.cookie('zy_token') == null){
									alert("请登录账户！")
									showlogin();
									return false;
						}
						var zNUM = T_num==0?1:T_num+1
						$(".build-num").text(zNUM)
						var shopName = $(this).parent().find("h3").text();
						var shopId = $(this).parent().find("h3").attr("id");
						$(".shopName").text(shopName)
						$(".shopName").attr("id",shopId)
						$(".map-build-box").show();
						$(".map-pic-shade").show();
		   })
		   //弹窗关闭事件
		   $(".build-colse").click(function(){
						clearData()
			})
		   	// 提交判断
			$(".build-next,.build-checkBtn").click(function(){									
						if($(".build-area").val() ==""){
								alert("请输入描述文字！")
								return false;
						}
						if($(".build-picArea img").attr("src") == ""){
									alert("请上传图片！")
									return false;
						}
						var T_sid = $(".shopName").attr("id");
						T_num++;
						var T_name = $(".shopName").text();
						var T_area = $(".build-area").val();
						var T_pic = $(".build-picArea img").attr("src")
						createl(T_num,T_pic,T_name,T_area)
						arrPush(T_sid,T_pic,T_area)
						console.log(T_num)
						if(T_num==2){
						 setTimeout(function (){
						  $('.build-next').hide();
                          $(".build-checkBtn").show();
						 },1000)
						};
			})
			// 跳转到已生成的攻略
			function scrollingToGuide(callback){
			   var offsetTop = $('#map-prompt').offset().top + "px";
			   $("html, body").animate({
					scrollTop: offsetTop
				},'500',"swing",callback
				);
			}
			// 触发弹窗事件
			function buildWindow(){
					$(".map-build-box").show();
			}
			function clearData(){
				    if(T_num == 1){
					  $('#guide-title').show();
					}
					if(T_num==3){
				      $(".map-build-box").fadeOut('fast',function (){
				        scrollingToGuide();
				      });
					}
					else{
						$(".map-build-box").fadeOut('fast');
					}
					$(".map-pic-shade").fadeOut('fast');
					$(".build-area").val("");
					$(".build-picArea img").attr("src","")

			}
			function arrPush(shopid,img,info){
						arrShopId.push(shopid)
						arrImg.push(img)
						arrInfo.push(info)
			}
			function createl(Num,pic,shop,areaInfo){
						var $creatHtml = $('<div class="shop-NumList"><img class="creatNumPic" width="130" height="88" src="'+pic+'" /><div class="creatNumTil"><p class="shop-NumList-cor">第<span class="creat-num">'+ Num +'</span>站<span>'+shop+'</span></p><div class="areaInfo">'+areaInfo+'</div></div></div>')
						$(".build-accomplish-list").append($creatHtml)
						clearData()
						$(".tijiao-btn").show();
			}
			// 攻略提交
			$(".tijiao-btn").click(function(){
						var idOutput = arrShopId.join(",")
						var imgOutput = arrImg.join(",")
						var infoOutput = arrInfo.join("$$$$$$")
						var tok = $.cookie('zy_token')
                        var title= $("#title").val() 
						gonglue(tok,"1",idOutput,imgOutput,infoOutput,title)
			})
			function gonglue(token,n,sids,imgs,intro,title){
					if($("#title").val() ==""){
								alert("所创建攻略名称不能为空！");
								return false;
						}
				    else if(arrImg.length<3){
				        alert("所创建攻略需要选择三个地方才能生成，当前只选择了"+arrImg.length+"个地方！")
								return false;
				    }
				var	ajax_gonglue = {
								action:"create",
                                title:title,
								cata:n,
								token:token,
								imgs:imgs,
								sids:sids,
								intro:intro
					}
					$.ajax({
						 url:"/Gplugin/Gonglue.ashx",
						 data:ajax_gonglue,
						 success:function(data){
								  var gonglueHtml = eval("("+ data +")")
								  if(gonglueHtml.result=="success"){
										  	alert("创建成功！")
											//var msg="我刚刚在#樟宜攻略#中分享了自己在机场内的快乐体验，如果你也想和我一样在樟宜大快朵颐、满载而归， 就快来@新加坡樟宜机场官方微博 阅读达人攻略并创建自己的心得体验吧！"
							
											var msg="我刚刚在#樟宜攻略#中分享了自己在机场内的快乐体验，如果你也想和我一样在樟宜大快朵颐、满载而归， 就快来@新加坡樟宜机场官方微博 阅读达人攻略并创建自己的心得体验吧！"
											var shareurl = "http://www.zhangyijichuang/guide/build.shtml"
											var tempwindow = window.open('_blank');
											tempwindow.location = "http://service.weibo.com/share/share.php?appkey=2103685750&title=" + encodeURIComponent(msg) + shareurl + "&pic=" + imgs.split(",")[0];
											//window.location="http://service.weibo.com/share/share.php?appkey=2103685750&title="+encodeURIComponent(msg)+shareurl+"&pic="+imgs.split(",")[0];
								  }
								  if(gonglueHtml.result == "error"){
									  alert(gonglueHtml.msg)
								  }
								  
						  }
				  })
			}
})